package com.sfddata.musicpillow.oauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfddata.musicpillow.common.utils.PageUtils;
import com.sfddata.musicpillow.common.utils.Query;
import com.sfddata.musicpillow.oauth.dao.UserInfoDao;
import com.sfddata.musicpillow.oauth.entity.UserInfoEntity;
import com.sfddata.musicpillow.oauth.service.UserInfoService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("userInfoService")
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfoEntity> implements UserInfoService {



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserInfoEntity> page = this.page(
                new Query<UserInfoEntity>().getPage(params),
                new QueryWrapper<UserInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserInfoEntity getByAccount(String phone) {

        QueryWrapper<UserInfoEntity> qw = new QueryWrapper<>();
        qw.eq("user_account", phone);
        UserInfoEntity userInfoEntity = baseMapper.selectOne(qw);
        return userInfoEntity;
    }

    @Override
    public UserInfoEntity updateBabyInfo(UserInfoEntity userInfoVo) {

        QueryWrapper<UserInfoEntity> qw = new QueryWrapper<>();
        qw.eq("user_account", userInfoVo.getUserAccount());
        this.saveOrUpdate(userInfoVo, qw);

        UserInfoEntity userInfo = baseMapper.selectOne(qw);
        return userInfo;
    }

    @Override
    public UserInfoEntity insertIfDontExist(String phoneCheck) {

        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", phoneCheck);
        UserInfoEntity userInfoEntity = this.baseMapper.selectOne(queryWrapper);
        if (userInfoEntity == null){
            // 新用户
            UserInfoEntity user = new UserInfoEntity();
            user.setUserAccount(phoneCheck);
            user.setUserName(phoneCheck);
            this.save(user);
            return user;
        }else {

            return userInfoEntity;
        }

    }

}